package com.gouaixin.bee.beeframework2.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

//import com.external.maxwin.view.XListView;
import com.ab.util.AbDialogUtil;
import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.activity.B15_faxinBaoyueActivity;
import com.gouaixin.bee.beeframework2.activity.B2_fensiActivity;
import com.gouaixin.bee.beeframework2.activity.B3_fangkeActivity;
import com.gouaixin.bee.beeframework2.activity.B5_wabaoActivity;
import com.gouaixin.bee.beeframework2.activity.B6_ziliaoActivity;
import com.gouaixin.bee.beeframework2.activity.B8_shezhiActivity;
import com.gouaixin.bee.beeframework2.activity.B9_zhengyouActivity;
import com.gouaixin.bee.beeframework2.bean.USER;
import com.gouaixin.bee.beeframework2.component.RoundImageView;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.gouaixin.bee.beeframework2.activity.B12_wanshanZiliaoActivity;
import com.gouaixin.bee.beeframework2.activity.B13_aiBiActivity;
import com.gouaixin.bee.beeframework2.activity.B13_yBiActivity;
import com.gouaixin.bee.beeframework2.activity.B14_vipTequanActivity;
import com.gouaixin.bee.beeframework2.activity.B16_yuanfenHongniangyueActivity_new;
import com.gouaixin.bee.beeframework2.activity.B17_shoujiYanzhengActivity;
import com.gouaixin.bee.beeframework2.activity.B4_tequanActivity;
import com.gouaixin.bee.beeframework2.activity.B7_xiangceActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.GetListener;


/**
 * Created by Administrator on 2015/3/6.
 */
public class M4_profileFragment extends Fragment {
    Context context = getActivity();
    //    XListView xlistview;
    View mainView;

    RoundImageView icon;//用户头像
    TextView name;//用户昵称
    TextView id;//用户id
    TextView yBi;//用户Y币
    TextView aiBi;//用户爱币
    TextView chongzhiYbi;//充值Y币（聊天用）
    TextView chongzhiAibi;//充值爱币（礼物用）

    LinearLayout vipTequan;//VIP特权
    LinearLayout faxinBaoyue;//发信包月
    LinearLayout yuanfenHongliang;//缘分红娘
    LinearLayout shoujiYanzheng;//手机验证
    LinearLayout wodeAibi;//我的爱币

    ImageView shoujiYanzheng_icon;//手机验证_图标
    TextView shoujiYanzheng_notify;//手机验证_提示

    LinearLayout fangke;//最近来访
    LinearLayout fensi;//谁关注我
    LinearLayout tequan;//特权服务
    LinearLayout wabao;//每日挖宝
    LinearLayout ziliao;//个人资料
    LinearLayout xiangce;//我的相册
    LinearLayout zhengyou;//征友条件
    LinearLayout shezhi;//设置

    //VIP特权等的视图
    LinearLayout myView2[] = {vipTequan, faxinBaoyue, yuanfenHongliang, shoujiYanzheng, wodeAibi};

    //VIP特权等的id
    int myID2[] = {R.id.vipTequan, R.id.faxinBaoyue, R.id.yuanfenHongliang, R.id.shoujiYanzheng, R.id.wodeAibi};

    //最近来访、谁关注我等的视图
    LinearLayout myLinearLayout[] = {fangke, fensi, tequan, wabao, ziliao, xiangce, zhengyou, shezhi};

    //最近来访、谁关注我等的id
    int myid[] = {R.id.fangke, R.id.fensi, R.id.tequan, R.id.wabao, R.id.ziliao, R.id.xiangce, R.id.zhengyou, R.id.shezhi};


    //上传隐私照片、视频
    LinearLayout uploadVideo, uploadPicture, iconBg;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        xlistview= (XListView) inflater.inflate(R.layout.m4_profile_xlistview, null);

        mainView = inflater.inflate(R.layout.m4_profile, null);

//        xlistview.addHeaderView(mainView);


        initChongzhi();

        initZilizo();

        initKaiTong();


        return mainView;
    }

    @Override
    public void onResume() {
        super.onResume();

        initData();//初始化个人信息


    }

    /**
     * 初始化个人信息
     */
    private void initData() {
        BmobQuery<USER> query = new BmobQuery<USER>();
        query.getObject(getActivity(), BmobUser.getCurrentUser(getActivity()).getObjectId(), new GetListener<USER>() {
            @Override
            public void onSuccess(USER user) {
                USER myuser = user;
                BmobFile micon = myuser.getIcon();
                if (micon != null) {
                    String url = micon.getFileUrl(getActivity());
                    ImageLoader.getInstance().displayImage(url, icon);
                } else {
//                    icon.setImageResource(R.drawable.ic_user_avatar_default);
                    int sexType = PreferencesUtils.getInt(getActivity(), "sexType");
                    Log.e("test", "用户头像：icon为空，性别为(0男1女)：" + sexType);
                    switch (sexType) {
                        case 0:
                            icon.setImageResource(R.drawable.male_one);
                            break;
                        case 1:
                            icon.setImageResource(R.drawable.female_one);
                            break;
                    }

                }
                mLog.e("name:" + myuser.getName());
                mLog.e("id:" + myuser.getUsername());
                name.setText(myuser.getName());
                id.setText("id:" + myuser.getUsername());

                mLog.e("个人中心：" + new Gson().toJson(myuser));

                int ybiType = myuser.getYbiType();
                mLog.e("我的Y币：" + ybiType);

//                //当是VIP时，隐藏上传隐私照片和上传视频
                uploadPicture = (LinearLayout) mainView.findViewById(R.id.uploadpicture);
                uploadVideo = (LinearLayout) mainView.findViewById(R.id.uploadvideo);


                long vipEndTime = myuser.getVipEndTime();
                long currentTime = System.currentTimeMillis();
//                //是VIp
                if (currentTime <= vipEndTime) {
                    mLog.e("myuser.getVipEndTime()=" + "隐藏视频");
                    uploadPicture.setVisibility(View.GONE);
                    uploadVideo.setVisibility(View.GONE);
                }

//                }
//                //不是vip
                else {
                    mLog.e("myuser.getVipEndTime()=" + "显示视频");
                    uploadPicture.setVisibility(View.VISIBLE);
                    uploadVideo.setVisibility(View.VISIBLE);
                }
//                    MemberModel.vipSuccess(context, VipType.Type_0);
//                }


                yBi.setText("" + ybiType);
//                yBi.setText(myuser.getAbiType());
//
//                if (myuser.getYbiType() != 0 ) {
//                    int ybi=myuser.getYbiType();
//                    yBi.setText(ybi);
//                }else{
//                    yBi.setText("0");
//                }
//
//
//                if (myuser.getAibi() != null ) {
//                    String aibi=myuser.getAibi();
//                    aiBi.setText(aibi);
//                }else{
//                    aiBi.setText("0");
//                }

            }

            @Override
            public void onFailure(int i, String s) {

            }
        });

    }

    /**
     * 用户头像等、充值的监听
     */
    private void initChongzhi() {
        icon = (RoundImageView) mainView.findViewById(R.id.icon);
        iconBg = (LinearLayout) mainView.findViewById(R.id.iconBg);
        name = (TextView) mainView.findViewById(R.id.name);
        id = (TextView) mainView.findViewById(R.id.id);
        yBi = (TextView) mainView.findViewById(R.id.yBi);
        aiBi = (TextView) mainView.findViewById(R.id.aiBi);


        iconBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(getActivity(),"完善资料"+"功能正在开发中",Toast.LENGTH_SHORT).show();

                Intent intent2 = new Intent(getActivity(), B12_wanshanZiliaoActivity.class);
                intent2.putExtra("isMain", true);
                startActivity(intent2);
            }
        });

        chongzhiYbi = (TextView) mainView.findViewById(R.id.chongzhiYbi);
        chongzhiYbi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(getActivity(),"充值"+"功能正在开发中",Toast.LENGTH_SHORT).show();

                Intent intent2 = new Intent(getActivity(), B13_yBiActivity.class);
                startActivity(intent2);
            }
        });

        chongzhiAibi = (TextView) mainView.findViewById(R.id.chongzhiAibi);
        chongzhiAibi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(getActivity(),"充值"+"功能正在开发中",Toast.LENGTH_SHORT).show();

                Intent intent2 = new Intent(getActivity(), B13_aiBiActivity.class);
                startActivity(intent2);
            }
        });
    }

    /**
     * VIP特权等的监听
     */
    private void initKaiTong() {
        for (int i = 0; i < myView2.length; i++) {
            myView2[i] = (LinearLayout) mainView.findViewById(myID2[i]);
        }
        for (LinearLayout ll : myView2) {
            ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        switch (v.getId()) {
                            case R.id.vipTequan:
//                                Toast.makeText(getActivity(),"VIP特权"+"功能正在开发中",Toast.LENGTH_SHORT).show();
                                Intent k1 = new Intent(getActivity(), B14_vipTequanActivity.class);
                                startActivity(k1);

                                break;
                            case R.id.faxinBaoyue:
//                                Toast.makeText(getActivity(),"发现包月"+"功能正在开发中",Toast.LENGTH_SHORT).show();

                                Intent k2 = new Intent(getActivity(), B15_faxinBaoyueActivity.class);
                                startActivity(k2);
                                break;
                            case R.id.yuanfenHongliang:
//                                Toast.makeText(getActivity(),"缘分吧红娘"+"功能正在开发中",Toast.LENGTH_SHORT).show();

                                Intent k3 = new Intent(getActivity(), B16_yuanfenHongniangyueActivity_new.class);
                                startActivity(k3);

                                break;
                            case R.id.shoujiYanzheng:
//                                Toast.makeText(getActivity(),"手机验证"+"功能正在开发中",Toast.LENGTH_SHORT).show();

                                Intent k4 = new Intent(getActivity(), B17_shoujiYanzhengActivity.class);
                                k4.putExtra("title", "手机验证");
                                startActivity(k4);

                                break;

                            case R.id.wodeAibi:
//                                Toast.makeText(getActivity(),"手机验证"+"功能正在开发中",Toast.LENGTH_SHORT).show();

                                Intent intent2 = new Intent(getActivity(), B13_aiBiActivity.class);
                                startActivity(intent2);

                                break;


                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        shoujiYanzheng_icon= (ImageView) mainView.findViewById(R.id.shoujiYanzheng_icon);
        shoujiYanzheng_notify= (TextView) mainView.findViewById(R.id.shoujiYanzheng_notify);

        String phone=PreferencesUtils.getString(getActivity(),"phone","");
        mLog.e("手机验证：phone="+phone);
        if(phone.equals("")){
            shoujiYanzheng_icon.setImageResource(R.drawable.ic_user_phone_nor);
            shoujiYanzheng_notify.setText("立即验证");
        }else{
            shoujiYanzheng_icon.setImageResource(R.drawable.ic_user_phone_sel);
            shoujiYanzheng_notify.setText("已经验证");
        }
    }

    /**
     * 最近来访、谁关注我等的监听
     */
    private void initZilizo() {
        for (int i = 0; i < myLinearLayout.length; i++) {
            myLinearLayout[i] = (LinearLayout) mainView.findViewById(myid[i]);
        }

        uploadPicture = (LinearLayout) mainView.findViewById(R.id.uploadpicture);
        uploadVideo = (LinearLayout) mainView.findViewById(R.id.uploadvideo);

        uploadPicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tanchuVip(1);

            }
        });

        uploadVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tanchuVip(2);

            }
        });


        for (LinearLayout ll : myLinearLayout) {
            ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        switch (v.getId()) {
                            case R.id.fangke:
//                                Toast.makeText(getActivity(),"最近来访"+"功能正在开发中",Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(getActivity(), B3_fangkeActivity.class);
                                startActivity(intent);
                                break;
                            case R.id.fensi:
//                                Toast.makeText(getActivity(),"谁关注我"+"功能正在开发中",Toast.LENGTH_SHORT).show();
                                Intent intent2 = new Intent(getActivity(), B2_fensiActivity.class);
                                startActivity(intent2);

                                break;
                            case R.id.tequan:
//                                Toast.makeText(getActivity(),"特权服务"+"功能正在开发中",Toast.LENGTH_SHORT).show();
                                Intent intent3 = new Intent(getActivity(), B4_tequanActivity.class);
                                startActivity(intent3);

                                break;
                            case R.id.wabao:
//                                Toast.makeText(getActivity(),"每日挖宝"+"功能正在开发中",Toast.LENGTH_SHORT).show();

                                Intent intent4 = new Intent(getActivity(), B5_wabaoActivity.class);
                                startActivity(intent4);

                                break;
                            case R.id.ziliao:
//                                Toast.makeText(getActivity(),"个人资料"+"功能正在开发中",Toast.LENGTH_SHORT).show();

                                Intent intent5 = new Intent(getActivity(), B6_ziliaoActivity.class);
                                intent5.putExtra("isMe", true);
                                intent5.putExtra("HerId", "");
                                startActivity(intent5);

                                break;
                            case R.id.xiangce:
//                                Toast.makeText(getActivity(),"我的相册"+"功能正在开发中",Toast.LENGTH_SHORT).show();

                                Intent intent7 = new Intent(getActivity(), B7_xiangceActivity.class);
                                startActivity(intent7);

                                break;
                            case R.id.zhengyou:
//                                Toast.makeText(getActivity(),"征友条件"+"功能正在开发中",Toast.LENGTH_SHORT).show();

                                Intent intent9 = new Intent(getActivity(), B9_zhengyouActivity.class);
                                startActivity(intent9);

                                break;
                            case R.id.shezhi:
//                                Toast.makeText(getActivity(),"设置"+"功能正在开发中",Toast.LENGTH_SHORT).show();

                                Intent intent8 = new Intent(getActivity(), B8_shezhiActivity.class);
                                startActivity(intent8);

                                break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }


    }

    private void tanchuVip(int flag) {

        mLog.e("弹出VIP服务对话框");


        View mView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_vipfuwu, null);
        AbDialogUtil.showDialog(mView);
        TextView content = (TextView) mView.findViewById(R.id.content);
        TextView quxiao = (TextView) mView.findViewById(R.id.quxiao);
        TextView kaitong = (TextView) mView.findViewById(R.id.kaitong);

        switch (flag) {
            case 1:
                content.setText("你还没有开通VIP，无法上传隐私照片！");
                break;
            case 2:
                content.setText("你还没有开通VIP，无法上传视频！");
                break;
        }

        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbDialogUtil.removeDialog(getActivity());
            }
        });
        kaitong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent it = new Intent().setClass(getActivity(), B14_vipTequanActivity.class);
                startActivity(it);
                AbDialogUtil.removeDialog(getActivity());
            }
        });
    }
}